<template>
	<view class="visit-info">
		<text v-if="visitorAmount" class="visit-amount">共{{visitorAmount}}个人访问</text>
		<view class="avatar-list">
			<view class="avatar-item" v-for="item in loadedVisitorList" :key="item.id">
				<u-avatar :src="handleAvatar(item.user_avatar)" size="55rpx"></u-avatar>
			</view>
		</view>
		<More @more="handleMore" :status="moreStatus" >
			<view class="more-visitor">
				<text>更多</text>
				<u-icon name="arrow-down-fill" color="#909399" size="14"></u-icon>
			</view>
		</More>
		
		<view class="like" @click="handleLike">
			<view :class="['like-circle', isLike ? 'active': '']">
				<view class="dot"></view>
				<view class="dot"></view>
				<view class="dot"></view>
			</view>
			<image class="like-icon" :src="likeIcon" mode="scaleToFill"></image>
		</view>
		<text v-if="likeAmount" class="like-amount">共{{likeAmount}}人点赞</text>
	</view>
</template>

<script>
	import More from '@/components/common/More.vue'
	
	export default {
		name: "LikeInfo",
		components: {
			More
		},
		props: {
			// 点赞数量
			likeAmount: {
				type: Number
			},
			
			// 访客数量
			visitorAmount: {
				type: Number
			},
			
			// 是否已点赞
			isLike: {
				type: Boolean,
				default: false
			},
			
			// 访客分页列表
			visitorList: {
				type: Array
			},
			
			// 是否已加载全部
			isEnd: {
				type: Boolean,
				default: false
			},
			
			// 当前访客页数
			currPage: {
				type: Number
			},
			
		},
		data() {
			return {
				// 已加载的访客列表
				loadedVisitorList: [],
				// 加载更多组件的状态
				moreStatus: 'nomore',
				// 锁定点赞
				isLock: false
			};
		},
		watch: {
			isLike: function() {
				this.isLock = false
			},
			
			visitorList: function(list) {
				this.moreStatus = this.isEnd ? 'nomore': 'more'
				this.loadedVisitorList = this.loadedVisitorList.concat(this.visitorList)
			}
		},
		computed: {
			likeIcon() {
				return this.isLike ? '/static/imgs/like-icon@active.png' : '/static/imgs/like-icon.png'
			},
		},
		methods: {
			handleLike() {
				if(!this.isLock) {
					// 锁定点赞
					this.isLock = true
					// 触发事件
					this.$emit('like', !this.isLike)
				}
			},
			
			// 加载更多访客事件
			handleMore() {
				this.moreStatus = 'loading'
				
				const page = this.currPage + 1
				this.$emit('load', page)
			},
			
			// 处理用户头像
			handleAvatar(url) {
				let avatarUrl = url
				if(!avatarUrl) {
					avatarUrl = '/default-avatar'
				}
				
				return avatarUrl
			}
		}
	}
</script>

<style lang="scss" scoped>
	@keyframes rotate360 {
		0% {
			transform: rotate(0deg);
			transform-origin: center;
		}

		to {
			transform: rotate(1turn);
			transform-origin: center;
		}
	}

	.visit-info {
		box-sizing: border-box;
		padding: 0 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.visit-amount {
			font-size: 12px;
			color: #909399;
		}

		.avatar-list {
			margin: 10rpx 0;
			width: 100%;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;

			.avatar-item {
				width: 10%;
				height: 70rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				view {
					width: 55rpx;
					height: 55rpx;
					border-radius: 50%;
					background-position: center;
					background-repeat: no-repeat;
					background-size: cover;
				}
			}

			image {
				// margin: 8rpx 6rpx;
				border-radius: 50%;
				width: 10%;
				// height: 55rpx;
			}
		}

		.more-visitor {
			margin-top: 10rpx;
			display: flex;
			align-items: baseline;

			text {
				margin-right: 10rpx;
				color: #909399;
				font-size: 14px;
			}
		}

		.like {
			margin-top: 50rpx;
			position: relative;
			width: 200rpx;
			height: 200rpx;

			.like-circle {
				position: absolute;
				top: 0;
				left: 0;
				bottom: 0;
				right: 0;
				animation: rotate360 2s linear infinite forwards;

				background-position: center;
				background-repeat: no-repeat;
				background-size: contain;

				&.active {

					&::before {
						border-color: #273368;
					}

					&::after {
						border-color: #273368;
					}

					.dot {
						border-color: #273368;

						&::after {
							background: #273368;
						}
					}
				}

				&::before {
					box-sizing: border-box;
					content: "";
					width: 100%;
					height: 100%;
					position: absolute;
					border-radius: 50%;
					opacity: .2;
					top: 0;
					left: 0;
					border: 1px solid #797979;
				}

				&::after {
					// box-sizing: border-box;
					// content: "";
					// position: absolute;
					// margin: auto;
					// border-radius: 50%;
					// height: 75%;
					// width: 75%;
					// top: 50%;
					// left: 50%;
					// right: auto;
					// bottom: auto;
					// transform: translate(-50%, -50%);
					// border: 1px solid #797979;
					// opacity: .4;
				}

				.dot {
					display: inline-block;
					position: absolute;
					border-radius: 50%;
					height: 10px;
					width: 10px;
					border: 1px solid #797979;
					border-color: #797979;
					opacity: .2;

					&::after {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						margin: auto;
						content: "";
						z-index: 2;
						display: inline-block;
						background: #797979;
						border-radius: 50%;
						height: 4px;
						width: 4px;
					}

					&:nth-child(1) {
						top: 0;
						left: 50%;
						transform: translateX(-50%) translateY(-50%);
					}

					&:nth-child(2) {
						top: 66%;
						left: 0;
						transform: translateX(-25%) translateY(-50%);
					}

					&:nth-child(3) {
						top: 66%;
						right: 0;
						transform: translateX(25%) translateY(-50%);
					}
				}

			}

			.like-icon {
				width: 200rpx;
				height: 200rpx;
				position: absolute;
				top: 0;
				left: 0;
				transform: scale(.8);
			}
		}

		.like-amount {
			margin-top: 30rpx;
			font-size: 12px;
			color: #273368;
		}
	}
</style>
